package org.example.test.test;

/**
 * 验证回文串
 * 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后，短语正着读和反着读都一样。则可以认为该短语是一个 回文串
 */
public class IsPalindrome {
    public static void main(String[] args) {
        System.out.println(isPalindrome("race a car"));
    }

    public static boolean isPalindrome(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)){
                stringBuilder.append(c);
            } else if (Character.isLetter(c)) {
                stringBuilder.append(Character.toLowerCase(c));
            }
        }
        String s1 = stringBuilder.toString();
        String s2 = stringBuilder.reverse().toString();
        return s1.equals(s2);
    }
}
